package org.yoyo.classroom.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.yoyo.classroom.domain.po.Notice;
import org.yoyo.classroom.domain.query.NoticeQuery;
import org.yoyo.classroom.result.Result;
import org.yoyo.classroom.service.NoticeService;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2024/12/22/10:44
 **/
@RestController
@RequestMapping("/admin/info/notice")
public class NoticeController {

    @Autowired
    private ResourceLoader resourceLoader;

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/list")
    public Result<PageInfo<Notice>> list(NoticeQuery query) {
        PageInfo<Notice> pageInfo = noticeService.getNoticeList(query);
        return Result.success(pageInfo);
    }

    @PostMapping
    public Result<?> add(@RequestBody Notice notice) {
        int rows = noticeService.addNotice(notice);
        return rows > 0 ? Result.success(notice) : Result.error("新增公告失败");
    }

    @PutMapping
    public Result<?> update(@RequestBody Notice notice) {
        int rows = noticeService.updateNotice(notice);
        return rows > 0 ? Result.success(notice) : Result.error("修改公告失败");
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        int rows = noticeService.deleteNotice(id);
        return rows > 0 ? Result.success(id) : Result.error("删除公告失败");
    }

    @PutMapping("/status/{id}/{status}")
    public Result<?> updateStatus(
            @PathVariable Long id,
            @PathVariable String status) {
        int rows = noticeService.updateStatus(id, status);
        return rows > 0 ? Result.success(id) : Result.error("更新状态失败");
    }
    @PostMapping("/upload")
    public Result<String> uploadImage(@RequestParam("file") MultipartFile file,
                                      @RequestParam("articleId") Long articleId) {
        try {
            // 使用 ResourceLoader 获取 resources/static/notice 目录
            Resource resource = resourceLoader.getResource("classpath:static/notice");
            String uploadPath = resource.getFile().getAbsolutePath() + File.separator;

            // 确保目录存在
            File dir = new File(uploadPath);
            if (!dir.exists()) {
                boolean created = dir.mkdirs();
            }

            // 使用文章ID作为文件名
            String fileExtension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String fileName = articleId + fileExtension;

            // 保存文件
            File dest = new File(uploadPath + fileName);
            file.transferTo(dest);

            // 返回文件访问路径
            return Result.success("/notice/" + fileName);

        } catch (IOException e) {
            return Result.error("文件上传失败：" + e.getMessage());
        }
    }
}